<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_obat_interaksi extends CI_Model {
	var $table = 'interaksi';
	
	public function get_all(){
		$sql = "SELECT * FROM interaksi i 
		JOIN obat o ON i.KdObat = o.KdObat
		ORDER BY KdInteraksi DESC";
		return $sql;
	}

	public function search($input){
		$kelompok = $input['kelompok'];
		$nama = $input['nama'];

		
		if($kelompok != ""){
			if($nama != ""){
				#kelompok + nama
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE k1.KdKelompok = $kelompok				
				OR k2.KdKelompok = $kelompok
				AND 
				o1.NmObat like '%$nama%'
				OR o2.NmObat like '%$nama%'
				ORDER BY KdInteraksi DESC
				";
			}else{
				#kelompok
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE k1.KdKelompok = $kelompok
				OR k2.KdKelompok = $kelompok
				ORDER BY KdInteraksi DESC
				";
			}
		}else{
			if($nama != ""){
				#nama
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE o1.NmObat like '%$nama%'
				OR o2.NmObat like '%$nama%'
				ORDER BY KdInteraksi DESC
				";
			}else{
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok
				ORDER BY KdInteraksi DESC
				";
			}
		}
		return $sql;
	}

	public function store($input){
		$data = array(
			'KdObat'			=> $input['obat1'],
			'KdObat2'			=> $input['obat2'],
			'Interaksi'		=> $input['interaksi']
			);
		$do_save = $this->db->insert($this->table, $data);

		if($do_save){
			set_flash('success','Data interaksi obat berhasil ditambahkan');
			return true;
		}else{
			set_flash('danger','Data interaksi obat gagal ditambahkan');
			return false;
		}
	}

	public function get_by_id($id){
		$this->db->where('KdInteraksi', $id);
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($input){
		$data = array(
			'KdInteraksi'	=> $input['kode'],
			'KdObat'			=> $input['obat1'],
			'KdObat2'			=> $input['obat2'],
			'Interaksi'		=> $input['interaksi']
			);

		$this->db->where('KdInteraksi', $input['KdInteraksi']);
		$do_update = $this->db->update($this->table, $data);

		if($do_update){
			set_flash('success','Data interaksi obat berhasil diperbaharui');
			return true;
		}else{
			set_flash('danger','Data interaksi obat gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdInteraksi', $id);
		$do_delete = $this->db->delete($this->table);

		if($do_delete){
			set_flash('success','Data interaksi obat berhasil dihapus');
			return true;
		}else{
			set_flash('danger','Data interaksi obat gagal dihapus');
			return false;
		}
	}

	public function export(){
		$this->db->join('obat', 'obat.KdObat = interaksi.KdObat');
		$this->db->order_by('KdInteraksi', 'desc');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($filter, $keyword){
		if($filter != "null"){
			if($keyword != ""){
				#filter = keyword
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE k1.KdKelompok = $filter				
				OR k2.KdKelompok = $filter

				AND 
				
				o1.NmObat like '%$keyword%'
				OR o2.NmObat like '%$keyword%'
				ORDER BY KdInteraksi DESC
				";
				$query = $this->db->query($sql);				
				
			}else{
				#filter
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE k1.KdKelompok = $filter
				OR k2.KdKelompok = $filter
				ORDER BY KdInteraksi DESC
				";
				$query = $this->db->query($sql);
			}
		}else{ 
			if($keyword != ""){
				#keyword
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				WHERE o1.NmObat like '%$keyword%'
				OR o2.NmObat like '%$keyword%'
				ORDER BY KdInteraksi DESC
				";
				$query = $this->db->query($sql);
			}else{
				$sql = "
				SELECT KdInteraksi, i.KdObat, i.KdObat2, o1.NmObat, Interaksi 
				FROM interaksi i 
				JOIN obat o1 ON
				i.KdObat = o1.KdObat
				JOIN obat o2 ON
				i.KdObat2 = o2.KdObat

				JOIN kelompok k1 ON
				k1.KdKelompok = o1.KdKelompok
				JOIN kelompok k2 ON
				k2.KdKelompok = o2.KdKelompok

				ORDER BY KdInteraksi DESC
				";
				$query = $this->db->query($sql);
			}
		}

		if($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		}else{
			$result = array();
		}
		return $result;
	}
}
/* End of file m_obat_interaksi.php */
/* Location: ./application/models/m_obat_interaksi.php */